% This script will fill in both the extra Forward Guidance terms as well
% as rewrite the second sample priors in terms of the posterior means.
%
% Currently, it will get the data for the estimation you are running now.
%
% Last Edited: 5/20/2014 by TM

clc; clear
%% User defined 
cucd=cd; 
% Where to load from 
fillStru.loadPath=fullfile('BMJ_new','BaseDepU','pr5 twoWHours S1_94',...
    'modes runend 2007.50 csminwel pr5 twoWHours S1 05 20 14'); 

% Location and name of new xls data file 
fillStru.loadDataBook='FGTenOIS3P2WRealInfGCHours';
fillStru.loadDataLoc=cr_dirpath(cucd,'BMJ_new','data','New CFed DSGE'); 

% Location and name of new xls prior file
fillStru.loadBlankLoc=fullfile('BMJ_new','BaseDepU');
fillStru.loadBlankBook='baselineFillIn.xls';
fillStru.loadBlankSheet='prior';
fillStru.savePath=fullfile('BMJ_new','BaseDepU','pr5 twoWHours S2'); 

%% Load structures of estimation 
fprintf('Load Structures from previous Estimation...\n')
cd(fillStru.loadPath);
load workspace sampleStru dataStru model location priorStru flags parnames filterStru addsol
cd(cucd);
fprintf('------------------------------------------------------------\n')

%% Create new dataStru
dataStruNew=dataStru; 
dataStruNew.fileName=fillStru.loadDataBook; 
dataStruNew.loadpath=fillStru.loadDataLoc;


%% Remove Obs if used for the extended model analysis 
model.handle=modelHandleRestore(model.handle,'Obs'); 


%% Load new dataset, different filename, using settings in dataStru
fprintf('\nLoad New Dataset...\n\n')
[dataStruNew.Y,dataStruNew.Ynames,dataStruNew.sample,dataStruNew.trainvec]=...
    load_dset(dataStruNew.fileName,dataStruNew.loadPath,dataStruNew,...
    dataStruNew.demeanType,flags.onscreen);
fprintf('------------------------------------------------------------\n')

dataStruNew.data=dataStruNew.Y; 

%% Load Blank Priors
fprintf('\nFill in Blank Priors sheet...\n')
% Load XLS match NAN with estimated Parameter Values 
cd(fillStru.loadBlankLoc)
[~,~,Out.S2Prior] = xlsread(fillStru.loadBlankBook,fillStru.loadBlankSheet);
cd(cucd)

% Change "NaN's" in the Excel Sheet to NaN's in MATLAB.
% Note that I am just looking for strings, so be careful here.
Out.ReWriteParam = cell2mat(cellfun(@(x)(isa(x,'char')),Out.S2Prior(:,3),'UniformOutput',false));
Out.S2Prior(Out.ReWriteParam,3) = {nan};

%% Fill in the NAN with estimated values 
% Get list of Parameters to fill in.
Out.NanParam=Out.S2Prior(Out.ReWriteParam,1);

% Check that current names are in the same order as the new ones.
if ~isequal(parnames(ismember(parnames,Out.NanParam)),Out.NanParam)
    error('Parameters are not in the same order!')
end

% Fill in.  Note that we only want to fill in the parameters that we have
% not changed.
Out.S2Prior(ismember(Out.S2Prior(:,1),Out.NanParam),3) = num2cell(model.param(ismember(parnames,Out.NanParam)));

% Check if any nans
Temp.CheckNan = isnan(cell2mat(Out.S2Prior(:,3)));
if any(Temp.CheckNan)
    Temp.NanLoc = find(Temp.CheckNan);
    for i = 1:numel(Temp.NanLoc)
    fprintf('NaN at location %1.0f',Temp.NanLoc(i));
    end
    error(['Check NaN''s in ' paths_current.loadBlankBook])
end

Out.fullParVec = cell2mat(Out.S2Prior(:,3));
fprintf('------------------------------------------------------------\n')

%% Get KFStru
fprintf('\nGet KFLast...\n')
% Run KFilter with the extended parameter and the dataset loaded 
[KFStru,~]=feval(filterStru.funcKfilter,Out.fullParVec,model,dataStruNew,flags,filterStru);
KFLast.aZero=KFStru.aLast;
KFLast.pZero=KFStru.pLast;
KFLast.aZeroDecomp=squeeze(KFStru.countSt(end,:,:));
fprintf('------------------------------------------------------------\n')

%% Create the savePath if it does not exist 
fprintf('\nSave output...\n')
fillStru.savePath=cr_dir(cucd,fillStru.savePath);

%% Create Output
% Create XLS file with 
%  Prior tab
%  Starting value tab
%  Grid tab
% =========================================================================
% Create KFStru
cd(fillStru.savePath);
save KFLast KFLast;
xlswrite(priorStru.fileName,Out.S2Prior,'prior')
xlswrite(priorStru.fileName,Out.S2Prior(:,1),'startValue')
xlswrite(priorStru.fileName,Out.S2Prior,'grid')
cd(cucd);
fprintf('------------------------------------------------------------\n')
fprintf('Done!\n')

